package cn.shop.dys.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.activity.MainActivity2;
import cn.shop.dys.bean.ProductInfo;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.constant.DBConstants;
import cn.shop.dys.util.TimeUtil;
import cn.shop.lib_common.CommonUtil;


/**
 * 限时抢购，适配器，
 * @author zhangjun
 * @param <T>
 */
//TODO Bug：限时特价部分条目与服务器不一样，价格基本是一样的。
public class LimitBuyItemAdapter<T> extends BaseAdapter {

    private List<T> objects;
    private Context context;
    private LayoutInflater layoutInflater;
    private int mLeftTime;


    public LimitBuyItemAdapter(Context context, List<T> datas) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        if (datas != null) {
            objects = datas;
        } else {
            objects = new ArrayList<T>();
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public T getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.limit_buy_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        ViewHolder holdedr = (ViewHolder) convertView.getTag();
        holdedr.setPosition(position);
        initializeViews((T) getItem(position), (ViewHolder) holdedr);
        return convertView;
    }

    private void initializeViews(T object, ViewHolder holder) {
        //TODO implement
        ProductInfo ProductInfo = (ProductInfo) object;
        String picUrl = ProductInfo.getPic();
        String name = ProductInfo.getName();
        float price = ProductInfo.getPrice();
        float limitPrice = ProductInfo.getLimitPrice();
        mLeftTime = ProductInfo.getLeftTime();
        picUrl =CommonConstant.HOST + picUrl;
        Glide.with(context).load(picUrl).into(holder.ivLimitBuyItemPic);
        holder.tvLimitBuyItemTitle.setText(name);
        holder.tvLimitBuyItemPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.tvLimitBuyItemPrice.setText("￥" + price);
        holder.tvLimitBuyItemLimitprice.setText("￥" + limitPrice);
        if (mLeftTime > 0) {
            holder.llLimitBuyItemFastbuy.setOnClickListener(holder);
        } else {
            holder.llLimitBuyItemFastbuy.setEnabled(false);//不能点击
        }
    }

    protected class ViewHolder implements View.OnClickListener {
        private ImageView ivLimitBuyItemPic;
        private TextView tvLimitBuyItemTitle;
        private TextView tvLimitBuyItemPrice;
        private TextView tvLimitBuyItemLimitprice;
        private TextView tvLimitBuyItemRemainTime;
        private LinearLayout llLimitBuyItemFastbuy;
        private int position;
        private ProductInfo prod;
        private Runnable downTimeTask = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        Message msg = new Message();
                        msg.what = 1;
                        handler.sendMessage(msg);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        };
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        if (mLeftTime > 0) {
                            mLeftTime--;
                            if (mLeftTime < 0) {
                                mLeftTime = 0;
                            }
                        }
                        tvLimitBuyItemRemainTime.setText("" + TimeUtil.secToTime(mLeftTime));
                        break;
                }
            }
        };

        public ViewHolder(View view) {
            ivLimitBuyItemPic = (ImageView) view.findViewById(R.id.iv_limit_buy_item_pic);
            tvLimitBuyItemTitle = (TextView) view.findViewById(R.id.tv_limit_buy_item_title);
            tvLimitBuyItemPrice = (TextView) view.findViewById(R.id.tv_limit_buy_item_price);
            tvLimitBuyItemLimitprice = (TextView) view.findViewById(R.id.tv_limit_buy_item_limitprice);
            tvLimitBuyItemRemainTime = (TextView) view.findViewById(R.id.tv_limit_buy_item_remain_time);
            llLimitBuyItemFastbuy = (LinearLayout) view.findViewById(R.id.ll_limit_buy_item_fastbuy);
            tvLimitBuyItemTitle.setOnClickListener(this);
            CommonUtil.runOnThread(downTimeTask);
        }

        public void setPosition(int position) {
            this.position = position;
            prod = (ProductInfo) (objects.get(position));
        }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_limit_buy_item_pic:
                case R.id.ll_limit_buy_item_fastbuy:
                case R.id.tv_limit_buy_item_title:
                    //TODO 跳转到商品详情列表 ，会员价格，商品id传递过去，
                    CommonUtil.showSafeToast(context,"跳转商品详情界面");
                    /*Intent intent = new Intent(context, MainActivity2.class);
                    intent.putExtra(CommonConstant.LIST_BUY_PRO_KEY, prod);
                    context.startActivity(intent);*/
                    break;
            }
        }
    }
}
